using System.Collections;
using System.Collections.Generic;
using NodeCanvas;
using NodeCanvas.DialogueTrees;
using UnityEngine;

public class DialogueChoiceGUI : MonoBehaviour
{
	public GUISkin skin;

	private DialogueOptionsInfo currentOptions;

	private float timer;

	private void Awake()
	{
		EventHandler.Subscribe(this, DLGEvents.OnDialogueOptions);
		EventHandler.Subscribe(this, DLGEvents.OnDialoguePaused);
		EventHandler.Subscribe(this, DLGEvents.OnDialogueFinished);
		base.enabled = false;
	}

	private void OnDialogueOptions(DialogueOptionsInfo optionsInfo)
	{
		base.enabled = true;
		timer = optionsInfo.availableTime;
		currentOptions = optionsInfo;
		StopCoroutine("GUICountDown");
		if (timer > 0f)
		{
			StartCoroutine("GUICountDown");
		}
	}

	private void OnDialoguePaused()
	{
		OnDialogueFinished();
	}

	private void OnDialogueFinished()
	{
		base.enabled = false;
		currentOptions = null;
		StopCoroutine("GUICountDown");
	}

	private void OnGUI()
	{
		if (currentOptions == null || !Camera.main)
		{
			base.enabled = false;
			return;
		}
		GUI.skin = skin;
		float num = ((timer > 0f) ? 20 : 0);
		foreach (KeyValuePair<Statement, int> finalOption in currentOptions.finalOptions)
		{
			num += new GUIStyle("box").CalcSize(new GUIContent(finalOption.Key.text)).y;
		}
		Rect screenRect = new Rect(10f, (float)Screen.height - num - 10f, Screen.width - 20, num);
		GUILayout.BeginArea(screenRect);
		foreach (KeyValuePair<Statement, int> finalOption2 in currentOptions.finalOptions)
		{
			if (GUILayout.Button(finalOption2.Key.text, new GUIStyle("box"), GUILayout.ExpandWidth(true)))
			{
				StopCoroutine("GUICountDown");
				base.enabled = false;
				currentOptions.SelectOption(finalOption2.Value);
				return;
			}
		}
		if (timer > 0f)
		{
			float b = GUI.color.b;
			float g = GUI.color.g;
			b = timer / currentOptions.availableTime * 0.5f;
			g = timer / currentOptions.availableTime * 0.5f;
			GUI.color = new Color(1f, g, b);
			GUILayout.Box("...", GUILayout.Height(5f), GUILayout.Width(timer / currentOptions.availableTime * screenRect.width));
		}
		GUILayout.EndArea();
	}

	private IEnumerator GUICountDown()
	{
		while (timer > 0f)
		{
			timer -= Time.deltaTime;
			yield return null;
		}
		currentOptions = null;
	}
}
